﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace LanguageStudy
{
    public partial class LSForm : Form
    {
        // 文件处理
        private WordsHandle m_fh;

        public LSForm()
        {
            InitializeComponent();

            m_fh = new WordsHandle();
        }

        private void LSForm_Load(object sender, EventArgs e)
        {
            notifyIcon.Icon = this.Icon;
        }

        private void LSForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                HideToNotity();
            }
        }

        private string m_strDirectory = "c:\\";
        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog _dlgOpenFile = new OpenFileDialog();

            _dlgOpenFile.InitialDirectory = m_strDirectory;
            _dlgOpenFile.Filter = "有道导出文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
            _dlgOpenFile.FilterIndex = 1;
            _dlgOpenFile.RestoreDirectory = true;

            if (_dlgOpenFile.ShowDialog() == DialogResult.OK)
            {
                // 路径处理
                m_strDirectory = _dlgOpenFile.FileName;

                // 解析文件
                ArrayList _alWords = m_fh.ImportFile(_dlgOpenFile.FileName);
                if (_alWords == null)
                    return;

                // 添加词汇
                for (int _i = 0; _i < _alWords.Count; _i++)
                {
                    if (_alWords[_i] == null)
                        continue;
                    listBox.Items.Add((string)_alWords[_i]);
                }
            }
        }

        private void btnBuild_Click(object sender, EventArgs e)
        {
            ArrayList _alWords = m_fh.RandomWords(0);

            if (_alWords == null)
                return;

            richBox.Clear();
            for (int _i = 0; _i < _alWords.Count; _i++)
            {
                richBox.AppendText((string)_alWords[_i]);
                richBox.AppendText("   ");
            }
        }

        private void btmExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlgSaveFile = new SaveFileDialog();

            dlgSaveFile.InitialDirectory = m_strDirectory;
            dlgSaveFile.Filter = "文本文件 (*.txt)|*.txt";
            dlgSaveFile.FilterIndex = 1;
            dlgSaveFile.RestoreDirectory = true;

            if (dlgSaveFile.ShowDialog() == DialogResult.OK)
            {
                // 路径处理
                m_strDirectory = dlgSaveFile.FileName;

                // 写入文件
                try
                {
                    StreamWriter sw = new StreamWriter(dlgSaveFile.FileName);
                    sw.WriteLine(richBox.Text);
                    sw.Close();
                }
                catch (System.Exception ex)
                {
                    System.Console.WriteLine(ex);
                }
            }
        }

        private WSForm frmWS = null;
        private void btnShow_Click(object sender, EventArgs e)
        {
            if (m_fh.GetWords().Count <= 0)
                return;

            foreach (Form frm in Application.OpenForms)
            {
                if (frm.Name == "WSForm")
                {
                    return;
                }
            } 

            frmWS = new WSForm(m_fh);
            frmWS.Show();
            HideToNotity();
        }

        private void notifyIcon_Click(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.notifyIcon.Visible = false;
        }

        private void HideToNotity()
        {
            this.Hide();
            this.notifyIcon.Visible = true;
        }
    }
}